package com.jcodecraeer.xrecyclerview.progressindicator.indicator;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;

import java.util.ArrayList;
import java.util.List;

public class BallPulseSyncIndicator extends BaseIndicatorController {
    private float[] translateYFloats=new float[3];
    @Override
    public void draw(Canvas canvas, Paint paint) {
        float circleSpacing=4;
        float radius=(getWidth()-circleSpacing*2)/7;

        float x = getWidth()/ 2-(radius*2+circleSpacing);
        for (int i = 0; i < 3; i++) {
            canvas.save();
            float translateX=x+(radius*2)*i+circleSpacing*i;
            canvas.translate(translateX, translateYFloats[i]*15);
            canvas.drawCircle(0, getHeight()/2, radius, paint);
            canvas.restore();
        }
    }

    @Override
    public List<Animator> createAnimation() {
        List<Animator> animators=new ArrayList<>();
        int[] delays=new int[]{70,140,210};
        for (int i = 0; i < 3; i++) {
            final int index=i;
            AnimatorValue scaleAnim = new AnimatorValue();
            scaleAnim.setDelay(delays[i]);
            scaleAnim.setDuration(600);
            scaleAnim.setLoopedCount(-1);
            scaleAnim.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
                @Override
                public void onUpdate(AnimatorValue animatorValue, float v) {
                    translateYFloats[index] = v;
                    postInvalidate();
                }
            });

            scaleAnim.start();
            animators.add(scaleAnim);
        }
        return animators;
    }
}
